<!DOCTYPE html>
<html>
  <head>
    <title>
      Test if AudioContext state changes correctly after a source starts.
    </title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      const audit = new Audit.createTaskRunner();

      let autoplayIgnoresWebAudioEnabled;

      audit.define('autoplay-policy-setup', function(task) {
        autoplayIgnoresWebAudioEnabled =
            internals.runtimeFlags.autoplayIgnoresWebAudioEnabled;
        internals.runtimeFlags.autoplayIgnoresWebAudioEnabled = false;
        internals.settings.setAutoplayPolicy(
            'document-user-activation-required');
        task.done();
      });

      audit.define('test-context-state', function(task, should) {
        const context = new AudioContext();
        const osc = new OscillatorNode(context);
        osc.connect(context.destination);

        activateDocument().then(() => {
          should(context.state, 'Context state').beEqualTo('suspended');
          osc.start();
          should(context.state, 'Context state').beEqualTo('running');
          task.done();
        });
      });

      audit.define('post-test-cleanup', function(task) {
        internals.runtimeFlags.autoplayIgnoresWebAudioEnabled =
            autoplayIgnoresWebAudioEnabled;
        internals.settings.setAutoplayPolicy('no-user-gesture-required');
        task.done();
      });

      function activateDocument() {
        return new Promise((resolve, reject) => {
          chrome.gpuBenchmarking.pointerActionSequence([
            {
              source: 'mouse',
              actions: [
                { name: 'pointerDown', x: 1, y: 1 },
                { name: 'pointerUp' },
              ],
            }
          ], resolve);
        });
      }

      audit.run();
    </script>
  </body>
</html>
